<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5" v-if="route.query.levelId == 13">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['mall:mallMarketServiceRecord:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['mall:mallMarketServiceRecord:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="mallMarketServiceRecordList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="关联等级"
        align="center"
        prop="levelInfo.levelName"
      />
      <el-table-column
        label="手续费总额"
        align="center"
        prop="serviceTotalPrice"
      />
      <el-table-column label="分红人数" align="center" prop="personNum" />
      <el-table-column
        label="平均分红金额"
        align="center"
        prop="averagePrice"
      />
      <el-table-column label="分红时间" align="center" prop="createTime" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button link type="primary" @click="handleUpdate(scope.row)"
            >分红用户</el-button
          >

          <el-button
            link
            type="primary"
            @click="handleFenhong(scope.row)"
            v-if="route.query.levelId == 13 && scope.row.state == 0"
            >分红</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改创业合伙人分红日志对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form
        ref="mallMarketServiceRecordRef"
        :model="form"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="分红总金额" prop="serviceTotalPrice">
          <el-input
            v-model="form.serviceTotalPrice"
            placeholder="请输入手续费总额"
          />
        </el-form-item>
        <el-form-item label="金豆单价" prop="dayPrice">
          <el-input-number
            v-model="form.dayPrice"
            :precision="3"
            :step="0.001"
            :min="0"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="MallMarketServiceRecord">
import {
  listMallMarketServiceRecord,
  getMallMarketServiceRecord,
  delMallMarketServiceRecord,
  addMallMarketServiceRecord,
  updateMallMarketServiceRecord,
  bonusMallMarketServiceRecord,
} from "@/api/mall/mallMarketServiceRecord";
import { useRoute, useRouter } from "vue-router";

const { proxy } = getCurrentInstance();

const mallMarketServiceRecordList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const route = useRoute();

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    serviceTotalPrice: null,
    personNum: null,
    averagePrice: null,
    levelId: route.query.levelId,
  },
  rules: {},
});

const { queryParams, form, rules } = toRefs(data);

function handleFenhong(row) {
  proxy.$confirm("是否确认分红？").then(function () {
    const params = {
      id: row.id,
    };
    bonusMallMarketServiceRecord(params).then((response) => {
      proxy.$modal.msgSuccess("分红成功");
      getList();
    });
  });
}

/** 查询创业合伙人分红日志列表 */
function getList() {
  loading.value = true;
  listMallMarketServiceRecord(queryParams.value).then((response) => {
    mallMarketServiceRecordList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    createTime: null,
    serviceTotalPrice: null,
    personNum: null,
    averagePrice: null,
  };
  proxy.resetForm("mallMarketServiceRecordRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加创业合伙人分红日志";
}

const router = useRouter();

/** 修改按钮操作 */
function handleUpdate(row) {
  router.push({
    path: "/money/mallMarketServicePerson",
    query: { id: row.id },
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["mallMarketServiceRecordRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updateMallMarketServiceRecord(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addMallMarketServiceRecord(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除创业合伙人分红日志编号为"' + _ids + '"的数据项？')
    .then(function () {
      return delMallMarketServiceRecord(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "mall/mallMarketServiceRecord/export",
    {
      ...queryParams.value,
    },
    `mallMarketServiceRecord_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>
